//
//  MineViewController.m
//  HimalayaMusic
//
//  Created by dllo on 16/1/15.
//  Copyright © 2016年 WXCLegendary. All rights reserved.
//

#import "MineViewController.h"

@interface MineViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)NSArray *titleArray;
@property (nonatomic, strong)NSArray *imageArray;
@property (nonatomic, strong)UITableView *tableView;

@end

@implementation MineViewController

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


- (void)viewDidLoad {
    [super viewDidLoad];
self.navigationItem.title = @"我的";
    self.titleArray = @[@"我的收藏", @"清除缓存", @"夜间模式"];
    self.imageArray = @[@"heart", @"qingchu", @"night"];
    [self createTableView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor) name:@"nightMode" object:nil];
    
    
}

- (void)viewWillAppear:(BOOL)animated {
    
    //缓存大小需要改变
//    [self createTableView];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 200)];
    imageView.image = [UIImage imageNamed:@"mineHead2"];
    return imageView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 200;
}

//夜间模式通知调用的方法
- (void)changeColor {
     UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    NSString *judge = [[NSUserDefaults standardUserDefaults] valueForKey:@"nightMode"];
    
    if ([judge isEqualToString:@"night"]) {
        self.tableView.backgroundColor = [UIColor colorWithRed:29 / 255.0 green:40 / 255.0 blue:52 / 255.0 alpha:1];
       
        cell.backgroundColor = [UIColor colorWithRed:29 / 255.0 green:40 / 255.0 blue:52 / 255.0 alpha:1];
        cell.textLabel.textColor = [UIColor whiteColor];
        
    } else {
        self.tableView.backgroundColor = [UIColor whiteColor];
        cell.backgroundColor = [UIColor clearColor];
        cell.textLabel.textColor = [UIColor blackColor];
    }
}





- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height - 49) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];
    [self.tableView registerClass:[MineTableViewCell class] forCellReuseIdentifier:@"MineTableViewCellIdentifier"];
    [self.view addSubview:self.tableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
        cell.textLabel.text = @"我的收藏";
        cell.imageView.image = [UIImage imageNamed:@"heart"];
        
        
        
        
        
        
        
        
         return cell;
    } else if (indexPath.row == 1) {
        MineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MineTableViewCellIdentifier"];
        cell.leftImage.image = [UIImage imageNamed:@"qingchu"];
        cell.titleLabel.text = @"清除缓存";
        cell.switchControl.hidden = YES;
        
        // 找到缓存路径
        NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        cell.rightLabel.text = [NSString stringWithFormat:@"%.2fMB", [self folderSizeAtPath:cachesPath]];
        return cell;

    } else {
        MineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MineTableViewCellIdentifier"];
        cell.leftImage.image = [UIImage imageNamed:@"night"];
        cell.titleLabel.text = @"夜间模式";
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        CollectionViewController *collectionVC = [[CollectionViewController alloc] init];
        
        [self.navigationController pushViewController:collectionVC animated:YES];
    } else if (indexPath.row == 1) {
        //清除缓存
        [self clearCachesAction];
        MineTableViewCell *cell = (MineTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
         NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        cell.rightLabel.text = [NSString stringWithFormat:@"%.2fMB", [self folderSizeAtPath:cachesPath]];
    }
}

//清除缓存方法

- (void)clearCachesAction
{
    // 找到缓存路径
    NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    // 定义一个文件管理器
    NSError *error = nil;
    NSFileManager *manager = [NSFileManager defaultManager];
    // 当前路径下缓存文件夹删除
    
    UIAlertController *alter = [UIAlertController alertControllerWithTitle:@"提示" message:[NSString stringWithFormat:@"共释放%.2fMB空间", [self folderSizeAtPath:cachesPath]] preferredStyle:1];
    
    // 当前路径下缓存文件夹删除
    [manager removeItemAtPath:cachesPath error:&error];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:0 handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alter addAction:action];
    [self presentViewController:alter animated:YES completion:^{
        
    }];
}

- (long long) fileSizeAtPath:(NSString*) filePath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
}

//遍历文件夹获得文件夹大小，返回多少M
- (float ) folderSizeAtPath:(NSString*) folderPath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:folderPath]) return 0;
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    NSString* fileName;
    long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    return folderSize/(1024.0*1024.0);
}












- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
